//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using LitwareHR.PrivateApp.Presenters.ConfigCusto;
using Shp.Runtime.Contract;

public partial class ConfigCusto_EditView : BasePage, IEditViewView
{
    EditViewPresenter _presenter;
    EditViewPresenter Presenter
    {
        get
        {
            return _presenter;
        }
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);
        _presenter = new EditViewPresenter(this);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string idQS = Request.QueryString[Constants.ViewDefIdQS];
            if (string.IsNullOrEmpty(idQS))
                throw new ArgumentNullException(Constants.ViewDefIdQS);

            DefId = new Guid(idQS);

            if (Request.Cookies[Constants.EntityDefNameCookie] == null)
                throw new ArgumentNullException(Constants.EntityDefNameCookie);
            string entityDefName = Request.Cookies[Constants.EntityDefNameCookie].Value;
            if (string.IsNullOrEmpty(entityDefName))
                throw new ArgumentNullException(Constants.EntityDefNameCookie);

            Presenter.OnViewLoaded(Helpers.TenantAlias, entityDefName, DefId);
        }
    }

    private Guid DefId
    {
        get
        {
            if (ViewState[Constants.DefViewStateKey] == null)
                throw new ArgumentNullException();
            Guid defId = (Guid)ViewState[Constants.DefViewStateKey];
            return defId;
        }
        set
        {
            ViewState[Constants.DefViewStateKey] = value;
        }
    }

    private List<StringPair> ViewFields
    {
        get
        {
            List<StringPair> viewFields = ViewState[Constants.FieldsViewStateKey] as List<StringPair>;
            if (viewFields == null)
            {
                viewFields = new List<StringPair>();
                ViewState[Constants.FieldsViewStateKey] = viewFields;
            }
            return viewFields;
        }
        set
        {
            ViewState[Constants.FieldsViewStateKey] = value;
        }
    }

    protected void AddButton_Click(object sender, EventArgs e)
    {
        string key = LabelTextBox.Text;
        string value = FieldDropDown.SelectedValue;
        Presenter.OnAdd(key, value, this.ViewFields.ToArray());
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        Presenter.OnSave(DefId, this.ViewFields.ToArray());
    }

    protected void ViewFieldsGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string key = ViewFieldsGrid.Rows[e.RowIndex].Cells[0].Text;
        Presenter.OnRemove(key, this.ViewFields.ToArray());
    }

    public void ShowEntityFields(EntityField[] list)
    {
        FieldDropDown.DataSource = list;
        FieldDropDown.DataBind();
    }

    public void ShowViewFields(StringPair[] viewFields)
    {
        this.ViewFields = new List<StringPair>(viewFields);
        ViewFieldsGrid.DataSource = viewFields;
        ViewFieldsGrid.DataBind();
    }

}